/*
 * @description: 和app交互
 * @Author: wxy
 * @Date: 2019-12-06 14:15:58
 */
import dsBridge from '@/utils/dsbridge';

// 向app发送消息
function sendMsgToApp(fnName, data) {
  console.log(fnName, data);
  dsBridge.call(fnName, {
    app_key: 'web',
    Version: '1.1.0',
    data: data,
  });
}

// 控制顶部导航栏和底部标签栏   showNavigationBar顶部导航栏(1显示,0隐藏),showTabBar底部标签栏(1显示,0隐藏)
function initTools(showNavigationBar, showTabBar) {
  sendMsgToApp('UIApi.setNavAndTabbar', {
    showNavigationBar: showNavigationBar,
    showTabBar: showTabBar,
  });
}

// 不需要app状态栏
function removeStatusBar() {
  sendMsgToApp('UIApi.removeStatusBar', {
    needStatusBar: 'NO',
  });
}

// 需要app状态栏
function addStatusBar() {
  sendMsgToApp('UIApi.removeStatusBar', {
    needStatusBar: 'YES',
  });
}

// 关闭页面回到app
function closeWebview() {
  sendMsgToApp('UIApi.closeWebview', {});
}

// 绑定返回到app页面事件
function registerBackToApp() {
  dsBridge.register('onBackClickListener', () => {
    return {
      needsAPPSelfBack: '1',
    };
  });
}

// 解绑返回到app页面
function unregisterBackToApp() {
  dsBridge.register('onBackClickListener', () => {
    return {
      needsAPPSelfBack: '0',
    };
  });
}

let responseCallback;

// 调用app回复框 同事圈
function openAppResponsePopup(resPlaceholder, callback) {
  responseCallback = callback;
  sendMsgToApp('UIApi.webNeedAppShowKeyBoard', {
    maxSizeNum: 1000,
    placeholder: resPlaceholder,
  });
}

// 返回回复内容 同事圈
function registerResponse() {
  dsBridge.register('offerContentStrToWeb', function(data) {
    responseCallback(data);
  });
}

// 调用人员组件
function selectPerson(employeeNOArray, noEditEmployeeNOArray, maxNum) {
  let obj = {
    employeeNOArray: employeeNOArray,
    noEditEmployeeNOArray: noEditEmployeeNOArray,
  };
  if (maxNum) {
    obj.maxPeopleSize = maxNum;
  }
  sendMsgToApp('UIApi.webSelectPeople', obj);
}

// 返回选择的人
function registerSelectPeopleCompelete(callback) {
  dsBridge.register('selectPeopleCompelete', callback);
}

// 刷新页面
function registerRefresh() {
  dsBridge.register('refreshPage', function() {
    window.location.reload();
  });
}

export {
  initTools,
  sendMsgToApp,
  removeStatusBar,
  addStatusBar,
  closeWebview,
  registerBackToApp,
  unregisterBackToApp,
  openAppResponsePopup,
  registerResponse,
  selectPerson,
  registerSelectPeopleCompelete,
  registerRefresh,
};
